import java.util.HashMap;
import java.util.Iterator;
import smfl.advertisement;

/**
 * @author keithcasamento
 *
 */
public class InvestorAPI extends Database
{
    private Investor currInvestor; //locates current investor

    InvestorAPI()
    {}

    /**
     * If the investor is not found one needs to be created
     * @param int investors id
     */
    InvestorAPI(int id)
    {
        if(hasInvestor(id))
                currInvestor = getInvestor(id);
        else
                System.out.println("Investor not found, create one from main menu");
    }

    /**
     * Allows the creation of new Investor
     * @param String name of new investor
     * @param String email of new investor
     */
    InvestorAPI(String n, String em)
    {
        this.createInvestor(n, em);
    }

    /**
     *
     * @param int id of user to change to
     */
    public void changeCurrInv(int id)
    {
        if(hasInvestor(id)) {
                currInvestor = getInvestor(id);
                System.out.println("Logged in to User ID: " + id);
        }
        else
                System.out.println("User ID " + id + " not found");
    }

    /**
     * New investor's cash is initially set to $10,000
     * After creation the investor is added to the Database
     * @param String name of new investor
     * @param String email of new investor
     */
    public void createInvestor(String name, String email)
    {
        Investor inv = new Investor(name, email);
        inv.setID(getInvestorID());
        incrementInvestorID();

        addInvestor(inv);
        System.out.println("Use id " + inv.getID() + " to login");
    }

    /**
     *
     * @return double cash holdings
     */
    public double checkBalance()
    {
        return currInvestor.getCash();
    }

    /**
     *
     * @return void prints all currents stock holdings
     */
    public void checkHoldings()
    {
        displayAd();
        HashMap<String, Stock> investments = new HashMap<String, Stock>();
        investments = currInvestor.getHoldings();
        Iterator<Stock> i = investments.values().iterator();
        Stock temp;
        while(i.hasNext()) {
                temp = i.next();
                System.out.println("Symbol: " + temp.getTicker()
                                + "\nNumber of Shares: " + temp.getShares());
        }
    }

    /**
     *
     * @param int number of shares to be purchased
     * @param Sting stock ticker
     */
    public void buyStock(int numbOfShares, String ticker)
    {
        displayAd();
        double purchasePrice = 0.00;
        Quote q = new Quote(ticker); //get current market price
        if(q.price(ticker) == 1) { //if ticker was found
                purchasePrice = numbOfShares * q.getPrice();
                if( purchasePrice <= currInvestor.getCash() ) { //if user has enough money
                        currInvestor.subCash(purchasePrice);
                        Stock st = new Stock(numbOfShares, ticker);
                        currInvestor.addHolding(st);
                }
                else
                        System.out.println("Not enough money to proceed");
        }
        else
                System.out.println("Stock symbol not found!");
        System.out.println("You have bought " + numbOfShares + " shares of " + ticker + " for " + purchasePrice + ".");
    }

    /**
     *
     * @param int number of shares to sell
     * @param String stock ticker
     */
    public void sellStock(int numbOfShares, String ticker)
    {
        if(currInvestor.hasHolding(new Stock(numbOfShares, ticker)) ){
                double sellPrice;
                Quote q = new Quote(ticker); //get current price
                sellPrice = numbOfShares * q.getPrice();
                currInvestor.removeHolding(numbOfShares, ticker);
                currInvestor.addCash(sellPrice);
                System.out.println("You have sold " + ticker + " for $" + sellPrice);
        }
        else
                System.out.println("You do not own this stock or you do not have enough shares");
    }

    /**
     * Displays a random advertisement.
     */
     private void displayAd()
     {
         advertisement toDisplay = getAd();
         System.out.println(toDisplay.getText());
     }
}